package cn.qj.ymcc.service.impl;

import cn.qj.ymcc.api.LoginFeignAPI;
import cn.qj.ymcc.constants.BusinessConstants;
import cn.qj.ymcc.domain.Login;
import cn.qj.ymcc.domain.User;
import cn.qj.ymcc.domain.UserAccount;
import cn.qj.ymcc.domain.UserBaseInfo;
import cn.qj.ymcc.dto.CodeDto;
import cn.qj.ymcc.dto.RegisterParamsDto;
import cn.qj.ymcc.exception.GlobleBusinessException;
import cn.qj.ymcc.mapper.UserMapper;
import cn.qj.ymcc.result.JSONResult;
import cn.qj.ymcc.service.IUserAccountService;
import cn.qj.ymcc.service.IUserBaseInfoService;
import cn.qj.ymcc.service.IUserService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-07-23
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Autowired
    private LoginFeignAPI loginFeignAPI;
    @Autowired
    private IUserAccountService userAccountService;
    @Autowired
    private IUserBaseInfoService userBaseInfoService;
    @Override
    @GlobalTransactional
    public void phoneRegister(RegisterParamsDto registerParamsDto) {
        String phone = registerParamsDto.getMobile();
        String password = registerParamsDto.getPassword();
        Integer regChannel = registerParamsDto.getRegChannel();
        String smsCode = registerParamsDto.getSmsCode();
        //1.校验手机号
        if(StringUtils.isEmpty(phone)
        ||StringUtils.isEmpty(password)
        ||StringUtils.isEmpty(String.valueOf(regChannel))
        ||StringUtils.isEmpty(smsCode)){
            throw new GlobleBusinessException("参数不能为空");
        }
        //校验验证码 拿到key
        String key = BusinessConstants.PHONE_REGISTER + phone;
        //redis中获取验证码
        Object code = redisTemplate.opsForValue().get(key);
        //校验验证码是否存在
        if (code == null) {
            throw new GlobleBusinessException("短信验证码已过期,请重新获取");
        }
        //强转
        CodeDto value = (CodeDto) code;
        //校验验证码是否错误
        if (!smsCode.equals(value.getCode())) {
            throw new GlobleBusinessException("短信验证码错误");
        }
        //2.校验手机号是否存在
        Wrapper<User> www = new EntityWrapper<>();
        www.eq("phone", phone);
        User userDB = selectOne(www);
        if (userDB != null) {
            throw new GlobleBusinessException("手机号已存在");
        }
        //注册t_user t_user_account,t_user_base_info,t_login_log

        //先添加到t_login
        Login login = new Login();
        login.setUsername(phone);
        login.setPassword(password);
        login.setType(1);
        login.setEnabled(true);
        login.setAccountNonExpired(true);
        login.setAccountNonLocked(true);
        login.setCredentialsNonExpired(true);
        JSONResult jsonResult = loginFeignAPI.register(login);
        Object data = jsonResult.getData();
        String jsonString = JSONObject.toJSONString(data);
        Login LoginFromDB = JSONObject.parseObject(jsonString, Login.class);
        Long long_id = LoginFromDB.getId();
        //在根据login_id添加到t_user
        User user = new User();
        long now = new Date().getTime();
        user.setCreateTime(now);
        user.setUpdateTime(now);
        user.setPhone(phone);
        user.setNickName(phone);
        user.setBitState(0l);
        user.setSecLevel(0);
        user.setLoginId(long_id);
        insert(user);

        //t_user_account
        UserAccount userAccount = new UserAccount();
        userAccount.setCreateTime(now);
        userAccount.setUpdateTime(now);
        userAccount.setId(user.getId());
        userAccount.setUsableAmount(new BigDecimal(0));
        userAccount.setFrozenAmount(new BigDecimal(0));
        userAccountService.insert(userAccount);

        //t_user_base_info
        UserBaseInfo userBaseInfo = new UserBaseInfo();
        userBaseInfo.setCreateTime(now);
        userBaseInfo.setUpdateTime(now);
        userBaseInfo.setId(user.getId());
        userBaseInfo.setRegChannel(regChannel);
        userBaseInfoService.insert(userBaseInfo);
        //删除redis中的验证码
        redisTemplate.delete(key);
    }
}
